package cn.cyh.pay.service.impl;

import cn.cyh.basic.exception.BusinessException;
import cn.cyh.basic.service.impl.BaseServiceImpl;
import cn.cyh.order.service.IProductOrderService;
import cn.cyh.pay.domain.AlipayInfo;
import cn.cyh.pay.domain.PayBill;
import cn.cyh.pay.mapper.AlipayInfoMapper;
import cn.cyh.pay.mapper.PayBillMapper;
import cn.cyh.pay.service.IPayBillService;
import cn.cyh.pay.utils.AlipayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PayBillServiceImpl extends BaseServiceImpl<PayBill> implements IPayBillService {

    @Autowired
    private AlipayInfoMapper alipayInfoMapper;

    @Autowired
    private PayBillMapper payBillMapper;

    @Autowired
    private IProductOrderService productOrderService;

    @Override
    public String pay(PayBill payBill) {
        // 校验
        if(payBill == null){
            throw new BusinessException("支付数据为空");
        }

        PayBill bill = payBillMapper.loadById(payBill.getId());
        if (bill == null){
            throw new BusinessException("草");
        }
        // 区分支付类型
        switch (bill.getPayChannel().intValue()){
            case 1:// 支付宝
                AlipayInfo info = alipayInfoMapper.loadByShopId(bill.getShop_id());
                return AlipayUtils.pay(info,bill);
            case 2:// 微信
                break;
            case 3:// 银联
                break;
            default: // 余额
        }
        return null;
    }

    @Override
    public PayBill loadByUnionPaySn(String unionPaySn) {
       return payBillMapper.loadByUnionPaySn(unionPaySn);
    }
}
